<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckAES
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $originData = $request->getContent();

//        log_daily("fullurl")->info("请求头信息:".json_encode($request->header())."，请求地址:{$request->fullUrl()}， 请求原始参数:{$originData}");

        $all = json_decode(aes_cbc_decrypt($originData), true);

        if (! $all) {

            return [
                'code' => 1,
                'data' => [],
                'msg' => "加解密失败"
            ];
        }

        $request->merge($all);

        return $next($request);
    }
}
